package com.loyal.loyalerp.basedata.action.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.basedata.action.mapper.GyActionMapper;
import com.loyal.loyalerp.basedata.action.model.GyAction;
import com.loyal.loyalerp.basedata.action.service.ActionService;
import com.loyal.loyalerp.basedata.permission.mapper.GyPermissionMapper;
import com.loyal.loyalerp.utils.ValidateUtil;

@Service
public class ActionServiceImpl implements ActionService {
	@Autowired
	GyActionMapper gyActionMapper;
	
	@Autowired
	GyPermissionMapper gyPermissionMapper;

	@Override
	public PageInfo<GyAction> listGyAction(Integer PageNum, Integer recordNum, String sord, String sidx) {

		Page<GyAction> page = PageHelper.startPage(PageNum, recordNum);
		gyActionMapper.listSordGyAction(sidx,sord);
		PageInfo<GyAction> pageInfo = new PageInfo<GyAction>(page);
		return pageInfo;
	}

	@Override
	public int editGyAction(String oper, GyAction gyAction) {
		System.out.println(oper);
		if (oper.equals("add")) {
			
			if (!ValidateUtil.validateQuery("gy_action")) {
				return 600;
			}

			try {
				gyActionMapper.insertSelective(gyAction);
				return 200;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (oper.equals("del")) {
			
			if (!ValidateUtil.validateDel("gy_action")) {
				return 600;
			}
			
			String actioncode = gyActionMapper.listGyActionById(gyAction.getId());
			
			gyPermissionMapper.deleteByForeignKey(actioncode);
			
			try {
				gyActionMapper.deleteByPrimaryKey(gyAction.getId());
				return 200;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (oper.equals("edit")) {
			if (!ValidateUtil.validateEdit("gy_action")) {
				return 600;
			}
			try {
				gyActionMapper.updateByPrimaryKeySelective(gyAction);
				return 200;
			} catch (Exception e) {
				e.printStackTrace();
				return 888;
			}
		}

		return 500;
	}


	@Override
	public PageInfo<GyAction> findGyAction(Integer PageNum,Integer recordNum, String sord,String searchField, String searchString, String searchOper) {
		Page<GyAction> page = PageHelper.startPage(PageNum, recordNum);
		gyActionMapper.findGyAction(searchField, searchString,searchOper);
		PageInfo<GyAction> pageInfo = new PageInfo<>(page);
		return pageInfo;
	}

	@Override
	public List<GyAction> listAll() {
		return gyActionMapper.listGyAction();
	}

}
